package com.sigmazero13.bgjeeves.db;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

public enum DbTable {
	GAME_TABLE("games", new DbField[] {
		DbField.GAME_ID, DbField.GAME_NAME, DbField.GAME_MINPLAYERS, DbField.GAME_MAXPLAYERS,
		DbField.GAME_BGGLENGTH, DbField.GAME_BGGRATING, DbField.GAME_MANUAL
	}, 1),
	PLAY_TABLE("play_info", new DbField[] {
		DbField.PLAY_ID, DbField.PLAY_GAME_ID, DbField.PLAY_DATE, DbField.PLAY_LOCATION,
		DbField.PLAY_SCENARIO, DbField.PLAY_LENGTH, DbField.PLAY_COMMENTS
	}, 1),
	PLAYER_TABLE("play_player", new DbField[] {
		DbField.PLAYER_ID, DbField.PLAYER_PLAY_ID, DbField.PLAYER_NAME, DbField.PLAYER_USERNAME,
		DbField.PLAYER_TEAM, DbField.PLAYER_STARTINGPOS, DbField.PLAYER_SCORE, DbField.PLAYER_WINNER
	}, 1),
	CUSTOM_INFO_TABLE("game_custom_info", new DbField[] {
		DbField.CUSTOM_INFO_ID, DbField.CUSTOM_INFO_GAME_ID, DbField.CUSTOM_INFO_MINPLAYERS,
		DbField.CUSTOM_INFO_MAXPLAYERS
	}, 1),
	TEAM_TABLE("teams", new DbField[] {
		DbField.TEAM_ID, DbField.TEAM_GAME_ID, DbField.TEAM_NAME
	}, 5),
	TEAM_PLAYER_TABLE("team_player", new DbField[] {
		DbField.TEAM_PLAYER_ID, DbField.TEAM_PLAYER_TEAM_ID, DbField.TEAM_PLAYER_PLAYER_ID
	}, 5),
	
	SCENARIOS_VIEW("scenarios", new DbField[] {
		DbField.PLAY_GAME_ID, DbField.PLAY_SCENARIO
	}, 5),
	PLAYERS_WITH_TEAMS_VIEW("players_with_teams", new DbField[] {
		DbField.PLAYER_ID, DbField.PLAYER_PLAY_ID, DbField.PLAYER_NAME, DbField.PLAYER_USERNAME,
		DbField.TEAM_NAME, DbField.PLAYER_STARTINGPOS, DbField.PLAYER_SCORE, DbField.PLAYER_WINNER
	}, 5);
	
	
	private String tableName;
	private List<DbField> fields;
	private Integer startVersion;
	private Integer stopVersion;
	
	private DbTable(String tableName, DbField [] fields, Integer startVersion, Integer stopVersion) {
		this.tableName = tableName;
		this.fields = Arrays.asList(fields);
		this.startVersion = startVersion;
		this.stopVersion = stopVersion;
	}
	
	private DbTable(String tableName, DbField [] fields, Integer startVersion) {
		this(tableName, fields, startVersion, 999);
	}
	
	public String getName() {
		return tableName;
	}
	
	public Integer getStartVersion() {
		return startVersion;
	}
	
	public Integer getStopVersion() {
		return stopVersion;
	}
	
	public DbField [] getFields(Integer version) {
		List<DbField> versionFields = new ArrayList<DbField>();
		for (DbField field : fields) {
			if (field.getStartVersion() <= version && field.getStopVersion() >= version) {
				versionFields.add(field);
			}
		}
		return versionFields.toArray(new DbField[0]);
	}
	
	public DbField [] getCurrentFields() {
		return getFields(999);
	}
	
	public String[] getFieldNames() {
		List<String> names = new ArrayList<String>();
		for (DbField field : fields) {
			names.add(field.getName());
		}
		return names.toArray(new String[0]);
	}
	
	public String getIdFieldName() {
		return fields.get(0).getName();
	}
	
	public String getFullFieldName(DbField field) {
		if (!fields.contains(field)) {
			throw new IllegalArgumentException(field.getName() + " is not a member of " + tableName);
		}
		
		return tableName + "." + field.getName();
	}
}
